<?php
namespace suifeng\wechat;

/**
 *	微信公众平台PHP-SDK, ThinkPHP实例
 *  @author dodgepudding@gmail.com
 *  @link https://github.com/dodgepudding/wechat-php-sdk
 *  @version 1.2
 *  usage:
 *   $options = array(
 *			'token'=>'tokenaccesskey', //填写你设定的key
 *			'encodingaeskey'=>'encodingaeskey', //填写加密用的EncodingAESKey
 *			'appid'=>'wxdk1234567890', //填写高级调用功能的app id
 *			'appsecret'=>'xxxxxxxxxxxxxxxxxxx' //填写高级调用功能的密钥
 *		);
 *	 $weObj = new TPWechat($options);
 *   $weObj->valid();
 *   ...
 *
 */

class TPWechat extends WechatSDK
{
	/**
	 * log overwrite
	 * @see Wechat::log()
	 */
	protected function log($log){
		if ($this->debug) {
			if (function_exists($this->logcallback)) {
				if (is_array($log)) $log = print_r($log,true);
				return call_user_func($this->logcallback,$log);
			}elseif (class_exists('Log')) {
				Log::write('wechat：'.$log, Log::DEBUG);
				return true;
			}
		}
		return false;
	}

	/**
	 * 重载设置缓存
	 * @param string $cachename
	 * @param mixed $value
	 * @param int $expired
	 * @return boolean
	 */
	protected function setCache($cachename,$value,$expired){
	    return cache($cachename,$value,$expired);
	}

	/**
	 * 重载获取缓存
	 * @param string $cachename
	 * @return mixed
	 */
	protected function getCache($cachename){
		return cache($cachename);
	}

	/**
	 * 重载清除缓存
	 * @param string $cachename
	 * @return boolean
	 */
	protected function removeCache($cachename){
	    return cache($cachename,null);
	}

	public function getErrCode()
	{
	    return $this->errCode;
	}

	public function getErrMsg()
	{
	    $errMsg = WechatError::getErrText($this->errCode);
	    if ($errMsg) {
	        return $errMsg;
	    } else {
	       return $this->errMsg;
	    }
	}

	/**
	 * 获取黑名单。
	 *
	 * @param array $data 数据结构{"begin_openid":"OPENID1"}
	 * @return boolean|array
	 */
	public function get_blacklist($data){
	    if (!$this->access_token && !$this->checkAuth()) return false;
	    $result = $this->http_post(self::API_URL_PREFIX . '/tags/members/getblacklist?access_token=' . $this->access_token, self::json_encode($data));
	    if ($result) {
	        $json = json_decode($result,true);
	        if (!$json || !empty($json['errcode'])) {
	            $this->errCode = $json['errcode'];
	            $this->errMsg = $json['errmsg'];
	            return false;
	        }
	        return $json;
	    }
	    return false;
	}

	/**
	 * 加入黑名单。
	 *
	 * @param array $data 数据结构{"openid_list":["OPENID1”,” OPENID2”]}
	 * @return boolean|array
	 */
	public function add_blacklist($data){
	    if (!$this->access_token && !$this->checkAuth()) return false;
	    $result = $this->http_post(self::API_URL_PREFIX . '/tags/members/batchblacklist?access_token=' . $this->access_token, self::json_encode($data));
	    if ($result) {
	        $json = json_decode($result,true);
	        if (!$json || !empty($json['errcode'])) {
	            $this->errCode = $json['errcode'];
	            $this->errMsg = $json['errmsg'];
	            return false;
	        }
	        return $json;
	    }
	    return false;
	}

	/**
	 * 移除黑名单。
	 *
	 * @param array $data 数据结构{"openid_list":["OPENID1”,” OPENID2”]}
	 * @return boolean|array
	 */
	public function remove_blacklist($data){
	    if (!$this->access_token && !$this->checkAuth()) return false;
	    $result = $this->http_post(self::API_URL_PREFIX . '/tags/members/batchunblacklist?access_token=' . $this->access_token, self::json_encode($data));
	    if ($result) {
	        $json = json_decode($result,true);
	        if (!$json || !empty($json['errcode'])) {
	            $this->errCode = $json['errcode'];
	            $this->errMsg = $json['errmsg'];
	            return false;
	        }
	        return $json;
	    }
	    return false;
	}

	/**
	 * 获取标签列表。
	 *
	 * @return boolean|array
	 */
	public function get_tag_list()
	{
	    if (!$this->access_token && !$this->checkAuth()) return false;
	    $result = $this->http_get(self::API_URL_PREFIX . '/tags/get?access_token=' . $this->access_token);
	    if ($result) {
	        $json = json_decode($result,true);
	        if (!$json || !empty($json['errcode'])) {
	            $this->errCode = $json['errcode'];
	            $this->errMsg = $json['errmsg'];
	            return false;
	        }
	        return $json;
	    }
	    return false;
	}

	/**
	 * 获取标签粉丝列表。
	 *
	 * @param array $data 数据结构{"tagid": 134, "next_openid": ""}
	 * @return boolean|array
	 */
	public function get_tag_users($data)
	{
	    if (!$this->access_token && !$this->checkAuth()) return false;
	    $result = $this->http_post(self::API_URL_PREFIX . '/user/tag/get?access_token=' . $this->access_token, self::json_encode($data));
	    if ($result) {
	        $json = json_decode($result,true);
	        if (!$json || !empty($json['errcode'])) {
	            $this->errCode = $json['errcode'];
	            $this->errMsg = $json['errmsg'];
	            return false;
	        }
	        return $json;
	    }
	    return false;
	}

	/**
	 * 新建标签。
	 *
	 * @param array $data 数据结构{"tag": {"name" : "广东" }}
	 * @return boolean|array
	 */
	public function create_tag($data)
	{
	    if (!$this->access_token && !$this->checkAuth()) return false;
	    $result = $this->http_post(self::API_URL_PREFIX . '/tags/create?access_token=' . $this->access_token, self::json_encode($data));
	    if ($result) {
	        $json = json_decode($result,true);
	        if (!$json || !empty($json['errcode'])) {
	            $this->errCode = $json['errcode'];
	            $this->errMsg = $json['errmsg'];
	            return false;
	        }
	        return $json;
	    }
	    return false;
	}

	/**
	 * 编辑标签。
	 *
	 * @param array $data 数据结构{"tag": {"id" : 134, "name": "广东人"}}
	 * @return boolean|array
	 */
	public function update_tag($data)
	{
	    if (!$this->access_token && !$this->checkAuth()) return false;
	    $result = $this->http_post(self::API_URL_PREFIX . '/tags/update?access_token=' . $this->access_token, self::json_encode($data));
	    if ($result) {
	        $json = json_decode($result,true);
	        if (!$json || !empty($json['errcode'])) {
	            $this->errCode = $json['errcode'];
	            $this->errMsg = $json['errmsg'];
	            return false;
	        }
	        return $json;
	    }
	    return false;
	}

	/**
	 * 删除标签。
	 *
	 * @param array $data 数据结构{"tag": {"id" : 134}}
	 * @return boolean|array
	 */
	public function delete_tag($data)
	{
	    if (!$this->access_token && !$this->checkAuth()) return false;
	    $result = $this->http_post(self::API_URL_PREFIX . '/tags/delete?access_token=' . $this->access_token, self::json_encode($data));
	    if ($result) {
	        $json = json_decode($result,true);
	        if (!$json || !empty($json['errcode'])) {
	            $this->errCode = $json['errcode'];
	            $this->errMsg = $json['errmsg'];
	            return false;
	        }
	        return $json;
	    }
	    return false;
	}

	/**
	 * 批量为用户打标签。
	 *
	 * @param array $data 数据结构{"openid_list": ['', ''], "tagid": 134}
	 * @return boolean|array
	 */
	public function batch_tagging($data)
	{
	    if (!$this->access_token && !$this->checkAuth()) return false;
	    $result = $this->http_post(self::API_URL_PREFIX . '/tags/members/batchtagging?access_token=' . $this->access_token, self::json_encode($data));
	    if ($result) {
	        $json = json_decode($result,true);
	        if (!$json || !empty($json['errcode'])) {
	            $this->errCode = $json['errcode'];
	            $this->errMsg = $json['errmsg'];
	            return false;
	        }
	        return $json;
	    }
	    return false;
	}
}



